﻿#include "Util.h"


QString FitByteDisplayText(quint64 bytesize)
{
	qreal bytes = (qreal)bytesize;

	QString unit;		//显示单位

	if (bytes < 1KB)
	{
		unit = "B";
	}
	else if (bytes < 1MB)
	{
		bytes /= 1KB;
		unit = "KB";
	}
	else if (bytes < 1GB)
	{
		bytes /= 1MB;
		unit = "MB";
	}
	else
	{
		bytes /= 1GB;
		unit = "GB";
	}


	return QString("%1%2").arg(bytes,4,'f',2).arg(unit);
}

QString TransmissionSpeed(quint64 bytesize, quint64 elapsedtime)
{
	qreal speed = bytesize * 1000.0 / elapsedtime;

	QString unit;		//显示单位
	if (speed < 1KB)
	{
		unit = "B/s";
	}
	else if (speed < 1MB)
	{
		speed /= 1KB;
		unit = "KB/s";
	}
	else if (speed < 1GB)
	{
		speed /= 1MB;
		unit = "MB/s";
	}
	else
	{
		speed /= 1GB;
		unit = "GB/s";
	}

	return QString("%1%2").arg(speed,4,'f',2).arg(unit);
}

QString TransmitRemainingTime(quint64 bytesize, quint64 totalsize, quint64 elapsedtime)
{
	//计算剩余大小：
	qreal remainsize = static_cast<qreal> (totalsize - bytesize);
	//计算传输速度：
	qreal speed = bytesize * 1000.0 / elapsedtime;
	//计算剩余时间：
	qreal remaintime = remainsize / speed;

	QString unit;		//显示单位：

	if (remaintime < 60)
	{
		unit = "秒";
	}
	else if (remaintime < 60 * 60)
	{
		remaintime /= 60;
		unit = "分";
	}
	else if (remaintime < 60 * 60 * 24)
	{
		remaintime /= 60 * 60;
		unit = "小时";
	}
	else if (remaintime < 60 * 60 * 24 * 30)
	{
		remaintime /= 60 * 60 * 24;
		unit = "天";
	}
	else
	{
		remaintime /= 60 * 60 * 24 * 30;
		unit = "月";
	}

	return QString("%1%2").arg(remaintime,2,'f',3).arg(unit);
}
